package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.BizInspectionItem;
import com.ruoyi.system.mapper.BizInspectionTemplateMapper;
import com.ruoyi.system.domain.BizInspectionTemplate;
import com.ruoyi.system.service.IBizInspectionTemplateService;

/**
 * 检验方案Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-29
 */
@Service
public class BizInspectionTemplateServiceImpl implements IBizInspectionTemplateService 
{
    @Autowired
    private BizInspectionTemplateMapper bizInspectionTemplateMapper;

    /**
     * 查询检验方案
     * 
     * @param templateId 检验方案主键
     * @return 检验方案
     */
    @Override
    public BizInspectionTemplate selectBizInspectionTemplateByTemplateId(Long templateId)
    {
        return bizInspectionTemplateMapper.selectBizInspectionTemplateByTemplateId(templateId);
    }

    /**
     * 查询检验方案列表
     * 
     * @param bizInspectionTemplate 检验方案
     * @return 检验方案
     */
    @Override
    public List<BizInspectionTemplate> selectBizInspectionTemplateList(BizInspectionTemplate bizInspectionTemplate)
    {
        return bizInspectionTemplateMapper.selectBizInspectionTemplateList(bizInspectionTemplate);
    }

    /**
     * 新增检验方案
     * 
     * @param bizInspectionTemplate 检验方案
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBizInspectionTemplate(BizInspectionTemplate bizInspectionTemplate)
    {
        // 自动生成模板编号：IT+三位序号
        String maxNo = bizInspectionTemplateMapper.selectMaxTemplateNo();
        String nextNo = genNextTemplateNo(maxNo);
        bizInspectionTemplate.setTemplateNo(nextNo);
        // 创建人/创建时间
        bizInspectionTemplate.setCreatedBy(SecurityUtils.getUsername());
        bizInspectionTemplate.setCreatedTime(DateUtils.getNowDate());
        // 更新人/更新时间（初始同创建）
        bizInspectionTemplate.setUpdatedBy(SecurityUtils.getUsername());
        bizInspectionTemplate.setUpdatedTime(DateUtils.getNowDate());
        int rows = bizInspectionTemplateMapper.insertBizInspectionTemplate(bizInspectionTemplate);
        insertBizInspectionItem(bizInspectionTemplate);
        return rows;
    }

    /**
     * 修改检验方案
     * 
     * @param bizInspectionTemplate 检验方案
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBizInspectionTemplate(BizInspectionTemplate bizInspectionTemplate)
    {
        // 更新人/更新时间
        bizInspectionTemplate.setUpdatedBy(SecurityUtils.getUsername());
        bizInspectionTemplate.setUpdatedTime(DateUtils.getNowDate());
        bizInspectionTemplateMapper.deleteBizInspectionItemByTemplateId(bizInspectionTemplate.getTemplateId());
        insertBizInspectionItem(bizInspectionTemplate);
        return bizInspectionTemplateMapper.updateBizInspectionTemplate(bizInspectionTemplate);
    }

    /**
     * 批量删除检验方案
     * 
     * @param templateIds 需要删除的检验方案主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBizInspectionTemplateByTemplateIds(Long[] templateIds)
    {
        bizInspectionTemplateMapper.deleteBizInspectionItemByTemplateIds(templateIds);
        return bizInspectionTemplateMapper.deleteBizInspectionTemplateByTemplateIds(templateIds);
    }

    /**
     * 删除检验方案信息
     * 
     * @param templateId 检验方案主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBizInspectionTemplateByTemplateId(Long templateId)
    {
        bizInspectionTemplateMapper.deleteBizInspectionItemByTemplateId(templateId);
        return bizInspectionTemplateMapper.deleteBizInspectionTemplateByTemplateId(templateId);
    }

    /**
     * 新增检验项目明细信息
     * 
     * @param bizInspectionTemplate 检验方案对象
     */
    public void insertBizInspectionItem(BizInspectionTemplate bizInspectionTemplate)
    {
        List<BizInspectionItem> bizInspectionItemList = bizInspectionTemplate.getBizInspectionItemList();
        Long templateId = bizInspectionTemplate.getTemplateId();
        String templateNo = bizInspectionTemplate.getTemplateNo();
        if (StringUtils.isNotNull(bizInspectionItemList))
        {
            List<BizInspectionItem> list = new ArrayList<BizInspectionItem>();
            for (BizInspectionItem bizInspectionItem : bizInspectionItemList)
            {
                bizInspectionItem.setTemplateId(templateId);
                bizInspectionItem.setTemplateNo(templateNo);
                bizInspectionItem.setCreatedBy(SecurityUtils.getUsername());
                bizInspectionItem.setCreatedTime(DateUtils.getNowDate());
                list.add(bizInspectionItem);
            }
            if (list.size() > 0)
            {
                bizInspectionTemplateMapper.batchBizInspectionItem(list);
            }
        }
    }

    /**
     * 生成下一个模板编号：IT + 3位（如 IT001）
     */
    private String genNextTemplateNo(String maxNo) {
        try {
            if (StringUtils.isNotEmpty(maxNo) && maxNo.startsWith("IT")) {
                String numStr = maxNo.substring(2);
                int num = Integer.parseInt(numStr);
                int next = num + 1;
                return String.format("IT%03d", next);
            }
        } catch (Exception ignored) {}
        return "IT001";
    }
}
